<template>
  <div
    class="layout-container"
    :class="{
      'header-mode': layoutSettings.menuPosition === 'header',
      'mixed-mode': layoutSettings.menuPosition === 'mixed',
      collapsed: isCollapsed,
      'dark-mode': layoutSettings.isDarkMode,
      'hide-tags-view': !layoutSettings.showTagsView,
    }"
  >
    <!-- 左侧菜单模式 -->
    <template v-if="layoutSettings.menuPosition === 'aside'">
      <div class="sidebar-container" :class="{ collapsed: isCollapsed }">
        <SideMenu
          v-model:isCollapsed="isCollapsed"
          :showLogo="layoutSettings.showLogo"
          :activeMenu="activeMenu"
          @select="handleSelect"
        />
      </div>

      <div class="main-container">
        <div class="fixed-header">
          <Navbar :showCollapse="true" v-model:isCollapsed="isCollapsed" />
          <div v-if="layoutSettings.showTagsView" class="tags-view">
            <TagsView />
          </div>
        </div>
        <div class="app-main">
          <router-view />
        </div>
      </div>
    </template>

    <!-- 顶部菜单模式 -->
    <template v-if="layoutSettings.menuPosition === 'header'">
      <div class="main-container">
        <div class="fixed-header">
          <TopMenu
            :showLogo="layoutSettings.showLogo"
            :activeMenu="activeMenu"
            @select="handleSelect"
          />
          <div v-if="layoutSettings.showTagsView" class="tags-view">
            <TagsView />
          </div>
        </div>
        <div class="app-main">
          <router-view />
        </div>
      </div>
    </template>

    <!-- 混合菜单模式 -->
    <template v-if="layoutSettings.menuPosition === 'mixed'">
      <div class="mixed-layout">
        <MixedMenu
          :showLogo="layoutSettings.showLogo"
          :activeMenu="activeMenu"
          :showTagsView="layoutSettings.showTagsView"
          @select="handleSelect"
        >
          <template #tags-view>
            <TagsView />
          </template>
          <router-view />
        </MixedMenu>
      </div>
    </template>

    <!-- 悬浮设置按钮 -->
    <div class="layout-settings" style="position: fixed; top: 318px; right: 0">
      <el-button
        type="primary"
        circle
        class="settings-btn"
        @click="showDrawer = true"
      >
        <el-icon><Setting /></el-icon>
      </el-button>
    </div>

    <!-- 布局设抽屉 -->
    <LayoutSettings v-model="layoutSettings" v-model:visible="showDrawer" />
  </div>
</template>

<script setup>
import { ref, computed } from "vue";
import { useRouter, useRoute } from "vue-router";
import { Setting } from "@element-plus/icons-vue";
import { useAppStore } from "@/stores/app";
import TopMenu from "@/components/layout/TopMenu.vue";
import SideMenu from "@/components/layout/SideMenu.vue";
import TagsView from "@/components/layout/TagsView.vue";
import Navbar from "@/components/layout/Navbar.vue";
import LayoutSettings from "@/components/layout/LayoutSettings.vue";
import MixedMenu from "@/components/layout/MixedMenu.vue";

const router = useRouter();
const route = useRoute();
const appStore = useAppStore();

const activeMenu = computed(() => route.path);
const showDrawer = ref(false);
const isCollapsed = ref(false);

// 从 store 中获取布局设置
const layoutSettings = computed(() => ({
  menuPosition: appStore.menuPosition || 'aside',
  primaryColor: appStore.primaryColor || "#409EFF",
  isDarkMode: appStore.isDarkMode || false,
  showLogo: appStore.showLogo !== false,
  showTagsView: appStore.showTagsView !== false,
  menuBgColor: appStore.menuBgColor || "#001529",
  menuTextColor: appStore.menuTextColor || "#ffffff",
  menuActiveTextColor: appStore.menuActiveTextColor || "#409EFF",
  subMenuBgColor: appStore.subMenuBgColor || "#000c17",
}));

const tagsView = computed({
  get: () => store.state.settings.tagsView,
  set: (val) => {
    store.dispatch("settings/changeSetting", {
      key: "tagsView",
      value: val,
    });
  },
});


const handleSelect = (index) => {
  router.push(index);
};

// 监听布局设置变化
const handleLayoutChange = (val) => {
  appStore.setLayoutSettings(val);
};
</script>

<style scoped>
.layout-container {
  position: relative;
  width: 100%;
  height: 100vh;
  display: flex;
}

/* 左侧菜单容器 */
.sidebar-container {
  width: 220px;
  height: 100%;
  transition: width 0.3s;
  z-index: 1001;
  overflow: hidden;
  flex-shrink: 0;
}

.sidebar-container.collapsed {
  width: 64px;
}

/* 主容器 */
.main-container {
  flex: 1;
  min-height: 100%;
  position: relative;
  overflow: hidden;
  background: #f0f2f5;
  display: flex;
  flex-direction: column;
}

/* 固定头部 */
.fixed-header {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 9;
  width: calc(100% - 220px);
  transition: width 0.3s;
  background: #fff;
}

.layout-container.collapsed .fixed-header {
  width: calc(100% - 64px);
}

/* 顶部菜单模式下的固定头部 */
.layout-container.header-mode .fixed-header {
  width: 100%;
  left: 0;
}

/* 主内容区域 */
.app-main {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

/* 左侧菜单模式下的主内容区域 */
.layout-container:not(.header-mode) .app-main {
  /* margin-top: 64px; */
  /* 头部高度(60px) + 标签栏高度(34px) - 30px */
}

/* 顶部菜单模式下的主内容域 */
.layout-container.header-mode .app-main {
  /* margin-top: 74px;  */
  /* 头部菜单高度(60px) + 标签栏高度(34px) - 20px */
}

/* 用户信息 */
.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: all 0.3s;
}

.user-info:hover {
  background: rgba(0, 0, 0, 0.04);
}

.username {
  font-size: 14px;
  color: #606266;
}

/* 悬浮设置按钮 */
.float-setting {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 1000;
}

.float-setting .el-button {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transition: all 0.3s;
}

.float-setting .el-button:hover {
  transform: rotate(45deg);
}

/* 抽屉内容 */
.drawer-content {
  padding: 20px;
}

/* 响应式调整 */
@media screen and (max-width: 768px) {
  .username {
    display: none;
  }
}

/* 修改收缩按钮样式 */
.collapse-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  cursor: pointer;
  font-size: 16px;
  color: #909399;
  transition: all 0.3s;
  margin-right: 8px;
}

.collapse-btn:hover {
  color: #409eff;
  background-color: rgba(64, 158, 255, 0.1);
  border-radius: 4px;
}

.collapse-btn .el-icon {
  transition: transform 0.3s;
}

.collapse-btn .is-collapsed {
  transform: rotate(180deg);
}

/* 调整头部左侧布局 */
.header-left {
  display: flex;
  align-items: center;
  height: 100%;
}

.breadcrumb {
  margin-left: 8px;
}

/* 修改头部菜单模式下的布局 */
.layout-container.header-mode {
  display: flex;
  flex-direction: column;
  width: 100%;
}

.layout-container.header-mode .main-container {
  width: 100%;
}

/* 头部菜单模式下的固定头部 */
.layout-container.header-mode .fixed-header {
  width: 100%;
  left: 0;
}

/* 头部菜单容器样式 */
.home-header-menu {
  width: 100%;
  left: 0;
}

/* 主内容区域样式 */
.app-main {
  padding: 120px 20px 20px;
  min-height: calc(100vh - 120px);
  width: 100%;
  box-sizing: border-box;
}

/* 添加主题相关样式 */
.theme-list {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.theme-item {
  width: 40px;
  height: 40px;
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  transition: all 0.3s;
}

.theme-item:hover {
  transform: scale(1.05);
}

/* 添加全局夜间模式样式 */
:root {
  --el-bg-color: #fff;
  --el-bg-color-overlay: #fff;
  --el-text-color-primary: #303133;
  --el-text-color-regular: #606266;
  --el-border-color: #dcdfe6;
  --el-border-color-light: #e4e7ed;
  --el-fill-color-blank: #fff;
}

:root.dark-mode {
  --el-bg-color: #1a1a1a;
  --el-bg-color-overlay: #242424;
  --el-text-color-primary: #fff;
  --el-text-color-regular: #e5eaf3;
  --el-border-color: #333;
  --el-border-color-light: #4c4d4f;
  --el-fill-color-blank: #242424;

  /* 添加更多夜间模式变量 */
  --el-color-primary: #409eff;
  --el-color-success: #67c23a;
  --el-color-warning: #e6a23c;
  --el-color-danger: #f56c6c;
  --el-color-info: #909399;

  /* 组件背景色 */
  --el-component-bg-color: #242424;

  /* 文本颜色 */
  --el-text-color-secondary: #a8abb2;
  --el-text-color-placeholder: #8d8d8d;

  /* 边框颜色 */
  --el-border-color-darker: #636363;

  /* 填充色 */
  --el-fill-color: #303030;
  --el-fill-color-light: #262626;
  --el-fill-color-lighter: #1f1f1f;

  /* 禁用状态 */
  --el-disabled-bg-color: #1f1f1f;
  --el-disabled-text-color: #666;
  --el-disabled-border-color: #444;
}

/* 夜间模式下的全局样式 */
:root.dark-mode body {
  background-color: var(--el-bg-color);
  color: var(--el-text-color-primary);
}

/* 夜间模式下的组件样式 */
:root.dark-mode .el-button--default {
  --el-button-bg-color: var(--el-component-bg-color);
  --el-button-border-color: var(--el-border-color);
  --el-button-hover-bg-color: var(--el-fill-color);
  --el-button-hover-border-color: var(--el-border-color-light);
  --el-button-active-bg-color: var(--el-fill-color-darker);
  --el-button-active-border-color: var(--el-border-color-darker);
}

:root.dark-mode .el-input__wrapper {
  background: var(--el-component-bg-color);
  box-shadow: 0 0 0 1px var(--el-border-color) inset;
}

:root.dark-mode .el-table {
  --el-table-bg-color: var(--el-component-bg-color);
  --el-table-tr-bg-color: var(--el-component-bg-color);
  --el-table-header-bg-color: var(--el-fill-color);
  --el-table-border-color: var(--el-border-color);
}

:root.dark-mode .el-card {
  --el-card-bg-color: var(--el-component-bg-color);
  --el-card-border-color: var(--el-border-color);
}

:root.dark-mode .el-dialog {
  --el-dialog-bg-color: var(--el-component-bg-color);
  --el-dialog-border-color: var(--el-border-color);
}

:root.dark-mode .el-dropdown-menu {
  --el-dropdown-menu-bg-color: var(--el-component-bg-color);
  --el-dropdown-menuItem-hover-fill: var(--el-fill-color);
}

/* 设置抽屉样式 */
.layout-settings-drawer :deep(.el-drawer__header) {
  margin-bottom: 0;
  padding: 16px 20px;
  border-bottom: 1px solid var(--el-border-color-light);
}

.drawer-content {
  padding: 20px;
}

.setting-block {
  margin-bottom: 24px;
}

.setting-item-list {
  display: flex;
  gap: 12px;
  margin-bottom: 16px;
}

.setting-item {
  flex: 1;
  cursor: pointer;
  border-radius: 4px;
  overflow: hidden;
  border: 2px solid var(--el-border-color);
  transition: all 0.3s;
}

.setting-item.active {
  border-color: var(--el-color-primary);
}

.setting-item-preview {
  height: 80px;
  background: #f0f2f5;
  padding: 8px;
}

.aside-mode {
  display: flex;
  gap: 8px;
}

.preview-aside {
  width: 20%;
  height: 100%;
  background: #001529;
  border-radius: 2px;
}

.preview-main {
  flex: 1;
  background: #fff;
  border-radius: 2px;
}

.header-mode {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.preview-header {
  height: 20%;
  background: #001529;
  border-radius: 2px;
}

.preview-content {
  flex: 1;
  background: #fff;
  border-radius: 2px;
}

.setting-item-label {
  padding: 8px;
  text-align: center;
}

.theme-colors {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}

.color-block {
  aspect-ratio: 1;
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  transition: all 0.3s;
  border: 2px solid transparent;
}

.color-block:hover {
  transform: scale(1.05);
}

.color-block.active {
  border-color: #fff;
  box-shadow: 0 0 0 2px var(--el-color-primary);
}

.color-picker-wrapper {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.setting-switch {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.label {
  color: var(--el-text-color-regular);
}

.drawer-footer {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 20px;
  border-top: 1px solid var(--el-border-color-light);
  display: flex;
  gap: 12px;
  justify-content: space-between;
  background: #fff;
}

/* 夜间模式适配 */
.layout-container.dark-mode :deep(.setting-item-preview) {
  background: #1a1a1a;
}

.layout-container.dark-mode :deep(.preview-main),
.layout-container.dark-mode :deep(.preview-content) {
  background: #242424;
}

.layout-container.dark-mode :deep(.drawer-footer) {
  background: var(--el-bg-color);
}

/* 隐藏标签栏时的样式调整 */
.layout-container.hide-tags-view :deep(.app-main) {
  height: calc(100vh - 60px);
}

/* 修复混合模式布局 */
.mixed-layout {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.mixed-layout .main-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.mixed-layout .app-main {
  flex: 1;
  padding: 20px;
  box-sizing: border-box;
  overflow: auto;
}
</style> 